package com.itheima.app.manager;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.domain.db.*;
import com.itheima.service.db.BlackListService;
import com.itheima.service.db.NotificationService;
import com.itheima.service.db.QuestionService;
import com.itheima.service.db.UserInfoService;
import com.itheima.vo.SettingVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class SettingManager {

    @DubboReference
    private QuestionService questionService;

    @DubboReference
    private NotificationService notificationService;

    @DubboReference
    private BlackListService blackListService;

    @DubboReference
    private UserInfoService userInfoService;

    public ResponseEntity findSettingVo() {

        User user = UserHolder.get();

        Question byUserId = questionService.findByUserId(user.getId());

        Notification byUserId1 = notificationService.findByUserId(user.getId());

        SettingVo settingVo = new SettingVo();

        settingVo.setId(user.getId());

        settingVo.setPhone(user.getMobile());

        if (byUserId != null) {

            settingVo.setStrangerQuestion(byUserId.getStrangerQuestion());
        }

        if (byUserId1 != null) {

            settingVo.setPinglunNotification(byUserId1.getPinglunNotification());

            settingVo.setGonggaoNotification(byUserId1.getGonggaoNotification());

            settingVo.setLikeNotification(byUserId1.getLikeNotification());
        }

        return ResponseEntity.ok(settingVo);
    }

    public void setQuestion(String content) {

        User user = UserHolder.get();

        Question question = questionService.findByUserId(user.getId());

        if (question == null) {

            question = new Question();

            question.setUserId(user.getId());

            System.out.println(question);

            question.setStrangerQuestion(content);

            questionService.save(question);

        } else {

            question.setStrangerQuestion(content);

            questionService.update(question);
        }
    }

    public void setNotification(Notification param) {

        User user = UserHolder.get();

        Notification byUserId = notificationService.findByUserId(user.getId());

        if (byUserId == null) {

            param.setUserId(user.getId());

            notificationService.save(param);

        } else {

            byUserId.setGonggaoNotification(param.getGonggaoNotification());

            byUserId.setLikeNotification(param.getLikeNotification());

            byUserId.setPinglunNotification(param.getPinglunNotification());

            notificationService.update(byUserId);
        }
    }

    public ResponseEntity findBlackListByPage(Integer pageNum, Integer pageSize) {

        User user = UserHolder.get();

        PageBeanVo pageBeanVo = blackListService.findByPage(pageNum, pageSize, user.getId());

        List<BlackList> items = (List<BlackList>) pageBeanVo.getItems();

        List<UserInfo> userInfoList = new ArrayList<>();

        if (items != null && items.size() > 0) {

            for (BlackList item : items) {

                Long blackUserId = item.getBlackUserId();

                UserInfo byUserId = userInfoService.findByUserId(blackUserId);

                userInfoList.add(byUserId);
            }
        }

        pageBeanVo.setItems(userInfoList);

        return ResponseEntity.ok(pageBeanVo);
    }

    public void deleteBlackList(Long blackUserId) {

        User user = UserHolder.get();

        blackListService.delete(user.getId(),blackUserId);
    }
}
